<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  DataBoard,
  Menu,
  Reading,
  ChatLineRound,
  EditPen,
  User,
  SwitchButton,
  Fold,
  Expand,
  CaretBottom
} from '@element-plus/icons-vue'
import Sidebar from '@/components/SideBar.vue'
import avatar from '@/assets/default.png'
import { useTokenStore } from '@/stores/token.js'
import { useUserInfoStore } from '@/stores/userInfo.js'
import { useCateStore } from '@/stores/cate.js'
import { cateListService } from '@/api/cate.js'

// 类型定义
type DropdownCommand = 'dashboard' | 'cate' | 'book' | 'comment' | 'note' | 'user' | 'logout'

// Store 初始化
const tokenStore = useTokenStore()
const userInfoStore = useUserInfoStore()
const cateStore = useCateStore()

// 路由
const router = useRouter()

// 状态管理
const fold = ref(false)
const loading = ref(false)

// 获取分类列表
const fetchCateList = async () => {
  try {
    loading.value = true
    const { data } = await cateListService()
    cateStore.setCateLevel(data)
  } catch (error) {
    // ElMessage.error('获取分类列表失败')
  } finally {
    loading.value = false
  }
}

// 折叠/展开侧边栏
const toggleSidebar = () => {
  fold.value = !fold.value
}

// 下拉菜单命令处理
const handleCommand = (command: DropdownCommand) => {
  if (command === 'logout') {
    handleLogout()
  } else {
    router.push({ path: command })
  }
}

// 退出登录处理
const handleLogout = () => {
  ElMessageBox.confirm(
    '您确认要退出吗?',
    '温馨提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      tokenStore.removeToken()
      userInfoStore.removeInfo()
      router.push('/login')
      ElMessage.success('退出登录成功')
    })
    .catch(() => {
      ElMessage.info('用户取消了退出登录')
    })
}

// 生命周期钩子
onMounted(() => {
  fetchCateList()
})
</script>

<template>
  <el-container class="layout-container">
    <Sidebar :fold="fold" />
    
    <el-container>
      <el-header class="layout-header">
        <div class="header-left">
          <h1 class="title">后台管理系统</h1>
          
          <el-icon 
            :size="24" 
            class="fold-icon" 
            @click="toggleSidebar"
          >
            <component :is="fold ? Expand : Fold" />
          </el-icon>
        </div>
        
        <div class="header-right">
          <span class="welcome-text">
            欢迎登录，{{ userInfoStore.info?.username || '管理员' }}
          </span>
          
          <el-dropdown 
            trigger="click" 
            @command="handleCommand"
          >
            <span class="user-dropdown">
              <el-avatar 
                :src="userInfoStore.info?.userPic || avatar" 
                :size="36"
              />
              <el-icon class="dropdown-arrow">
                <CaretBottom />
              </el-icon>
            </span>
            
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="dashboard" :icon="DataBoard">
                  控制台
                </el-dropdown-item>
                <el-dropdown-item command="cate" :icon="Menu">
                  分类管理
                </el-dropdown-item>
                <el-dropdown-item command="book" :icon="Reading">
                  图书管理
                </el-dropdown-item>
                <el-dropdown-item command="review" :icon="ChatLineRound">
                  书评管理
                </el-dropdown-item>
                <el-dropdown-item command="note" :icon="EditPen">
                  笔记管理
                </el-dropdown-item>
                <el-dropdown-item command="user" :icon="User">
                  用户管理
                </el-dropdown-item>
                <el-dropdown-item 
                  command="logout" 
                  :icon="SwitchButton"
                  divided
                >
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-main class="layout-main">
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </el-main>
      
      <el-footer class="layout-footer">
        Admin System 2.0 | Copyright © 2025-Smart Reading
      </el-footer>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
  height: 100vh;
  overflow: hidden;
}

.layout-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 60px;
  background-color: #3f72af;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  z-index: 10;
  
  .header-left {
    display: flex;
    align-items: center;
    gap: 16px;
    
    .title {
      margin: 0;
      color: #fff;
      font-size: 18px;
      font-weight: 500;
      white-space: nowrap;
    }
    
    .fold-icon {
      color: #fff;
      cursor: pointer;
      transition: all 0.3s;
      
      &:hover {
        opacity: 0.8;
      }
    }
  }
  
  .header-right {
    display: flex;
    align-items: center;
    gap: 16px;
    
    .welcome-text {
      color: #fff;
      font-size: 14px;
      font-weight: 500;
    }
    
    .user-dropdown {
      display: flex;
      align-items: center;
      cursor: pointer;
      
      .dropdown-arrow {
        color: rgba(255, 255, 255, 0.7);
        margin-left: 8px;
        transition: transform 0.3s;
      }
      
      &:hover .dropdown-arrow {
        color: #fff;
      }
    }
  }
}

.layout-main {
  padding: 20px;
  background-color: #f0f2f5;
  overflow: auto;
}

.layout-footer {
  height: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 12px;
  color: #999;
  border-top: 1px solid #e8e8e8;
}

// 过渡动画
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>